package cn.kuick.avatar.utils;

import com.github.junrar.Archive;
import com.github.junrar.rarfile.FileHeader;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

/*
一些文件的基本操作
目前没有目录下查找文件
需要的包  compile group: 'com.github.junrar', name: 'junrar', version: '0.7'
*/

public class FileOperateUtils {
    public static Boolean deleteFile(String filePath){
        File file = new File(filePath);

        if(file.isFile() && file.exists()){
            file.delete();
            return true;
        }

        return false;
    }

    public static Boolean copyFile(String originFilePath, String targetFilePath){
        File originFile = new File(originFilePath);
        File targetFile = new File(targetFilePath);

        FileChannel inChannel = null;
        FileChannel outChannel = null;

        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;

        try{
            inputStream = new FileInputStream(originFile);
            outputStream = new FileOutputStream(targetFile);

            inChannel = inputStream.getChannel();
            outChannel = outputStream.getChannel();

            inChannel.transferTo(0, inChannel.size(), outChannel);

            inChannel.close();
            outChannel.close();
            inputStream.close();
            outputStream.close();
        } catch (IOException e){
            e.printStackTrace();

            return false;
        }

        return true;
    }

    public static boolean unRarFile(String originFilePath, String targetFolderPath){
        if(originFilePath.toLowerCase().endsWith(".rar") == false){
            System.out.println("不是rar文件！");
            return false;
        }

        File targetFolder = new File(targetFolderPath);
        if(targetFolder.exists() == false){
            targetFolder.mkdirs();
        }

        Archive archive =null;
        try {
            archive = new Archive(new File(originFilePath));
            if(archive != null){
                System.out.println("解压文件信息： ");
                archive.getMainHeader().print();

                FileHeader fileHeader = archive.nextFileHeader();
                while (fileHeader != null){
                    if(fileHeader.isDirectory()){
                        System.out.println("log1= " + targetFolderPath + File.separator + fileHeader.getFileNameString());
                        File file = new File(targetFolderPath + File.separator + fileHeader.getFileNameString());

                        file.mkdirs();

                    } else {
                        String fileName = fileHeader.getFileNameW().isEmpty()?fileHeader.getFileNameString():fileHeader.getFileNameW();
                        System.out.println(targetFolderPath + File.separator + fileName.trim());
                        File file = new File(targetFolderPath + File.separator + fileName.trim());

                        try{
                            if(file.exists() == false){

                                if(file.getParentFile().exists() == false){
                                    file.getParentFile().mkdirs();
                                }
                            }
                            file.createNewFile();

                            FileOutputStream fileOutputStream = new FileOutputStream(file);
                            archive.extractFile(fileHeader, fileOutputStream);

                            fileOutputStream.close();
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    }

                    fileHeader = archive.nextFileHeader();
                }

                archive.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return true;
    }

    public static boolean deleteDirectory(String directoryPath){
        if(!directoryPath.endsWith(File.separator)){
            directoryPath = directoryPath + File.separator;
        }

        File dirFile = new File(directoryPath);
        if(!dirFile.exists() || !dirFile.isDirectory()){
            return false;
        }

        File[] files = dirFile.listFiles();
        for (int i=0; i<files.length; i++){
            if(files[i].isFile()){
                files[i].delete();
            } else {
                deleteDirectory(files[i].getAbsolutePath());
            }
        }

        return dirFile.delete();
    }
}
